package com.stox.charting.ui.swing.model;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JComponent;

import com.stox.charting.ui.swing.view.ChartView;

public abstract class Axis extends JComponent{
	private static final long serialVersionUID = -8892088570740293805L;

	private final Orientation orientation;
	private final String borderLayoutConstraints;
	private final ChartView chartView;
	
	public Axis(ChartView chartView, Orientation orientation, String borderLayoutConstraint) {
		this.chartView = chartView;
		this.orientation = orientation;
		this.borderLayoutConstraints = borderLayoutConstraint;
		setOpaque(false);
	}
	
	public Orientation getOrientation() {
		return orientation;
	}

	public ChartView getChartView() {
		return chartView;
	}

	public String getBorderLayoutConstraints() {
		return borderLayoutConstraints;
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Rectangle r = getVisibleRect();
		int width = Theme.getStructure().getVerticalAxisWidth();
		g.setColor(Theme.getColours().getAxisLine());
		if(Orientation.VERTICAL.equals(orientation) 
				&& BorderLayout.WEST.equals(borderLayoutConstraints)){
			g.drawLine((int)(r.getX()+r.getWidth()-1), (int)r.getY(), (int)(r.getX()+r.getWidth()-1), (int)(r.getY()+r.getHeight()));
		}else if(Orientation.VERTICAL.equals(orientation) 
				&& BorderLayout.EAST.equals(borderLayoutConstraints)){
			g.drawLine((int)r.getX(), (int)r.getY(), (int)r.getX(), (int)(r.getY()+r.getHeight()));
		}else if(Orientation.HORIZONTAL.equals(orientation) 
				&& BorderLayout.NORTH.equals(borderLayoutConstraints)){
			g.drawLine((int)r.getX() + width, (int)(r.getY() + r.getHeight()-1), (int)(r.getX()+r.getWidth()-width), (int)(r.getY()+r.getHeight()-1));
		}else if(Orientation.HORIZONTAL.equals(orientation) 
				&& BorderLayout.SOUTH.equals(borderLayoutConstraints)){
			g.drawLine((int)r.getX() + width, (int)r.getY(), (int)(r.getX()+r.getWidth()-width), (int)r.getY());
		}
		
		
		
	}
	
	@Override
	public Dimension getSize() {
		if(getParent() != null){
			if(Orientation.HORIZONTAL.equals(getOrientation())){
				return new Dimension(getParent().getWidth(), 
						Theme.getStructure().getHorizontalAxisHeight());
			}else{
				return new Dimension(Theme.getStructure().getVerticalAxisWidth(), getParent().getHeight());
			}
		}
		return super.getSize();
	}
	
	
	@Override
	public Dimension getPreferredSize() {
		if(getParent() != null){
			if(Orientation.HORIZONTAL.equals(getOrientation())){
				return new Dimension(getParent().getWidth(), 
						Theme.getStructure().getHorizontalAxisHeight());
			}else{
				return new Dimension(Theme.getStructure().getVerticalAxisWidth(), getParent().getHeight());
			}
		}
		return super.getPreferredSize();
	}
	
}
